import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

ir_pin = 21
GPIO.setup(ir_pin, GPIO.IN)

cnt = 1 # avoid divided by zero
start_time = time.time()

# 初始状态
state = GPIO.input(ir_pin) 
while 1:
    nstate = GPIO.input(ir_pin)
    if state != nstate:
        cnt += 1
        state = nstate
    print(cnt, end = '\t|')
    print(round(cnt / (time.time() - start_time), 3),'cycle/second', end='')
    print(end='\r')